package com.dg.river.core.view.dialog;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dg.river.R;
import com.dg.river.core.util.ScreenUtils;
import com.dg.river.core.util.TextUtil;
import com.dg.river.core.util.ToastUtils;
import com.dg.river.core.util.Utils;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.Calendar;
import java.util.Locale;

/**
 * @作者：Hututu
 * @时间：2023/3/9 16:27
 * @描述：内容描述
 * @修改：修改内容
 * @Version:1.0
 */
public class OrderFilterDialog extends BottomSheetDialog {

    // 类型
    private int type = -1;

    // 选择时间
    private String time = "";

    // 开始时间 结束时间
    private String startTime = "";
    private String endTime = "";

    private LinearLayout llType;
    private ImageView ivClose;
    private TextView tvFinish;
    private TextView tvCancel;
    private TextView tvThree;
    private TextView tvSix;
    private TextView tvOneYear;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private TextView tvReset;
    private TextView tvQuery;

    private ChooseDateDialog chooseDateDialog;

    private OnQueryListener onQueryListener;

    public void setOnQueryListener(OnQueryListener onQueryListener) {
        this.onQueryListener = onQueryListener;
    }

    public interface OnQueryListener {
        void onQuery(OrderFilterDialog dialog, boolean hasFilter, String startTime, String endTime, int type, String time);
    }

    public OrderFilterDialog(@NonNull Context context) {
        super(context);
    }

    public OrderFilterDialog(@NonNull Context mContext, int theme, int orderType, int selectType, String selectTime, String selectStartTime, String selectEndTime) {
        super(mContext, theme);
        setDismissWithAnimation(true);
        if (orderType == 1) {
            getBehavior().setPeekHeight(ScreenUtils.dip2px(mContext, 618));
        } else if (orderType == 2) {
            getBehavior().setPeekHeight(ScreenUtils.dip2px(mContext, 527));
        }
        View view = getLayoutInflater().inflate(R.layout.dialog_date_sea_way, null);
        setContentView(view);
        // 关闭
        ivClose = view.findViewById(R.id.ivClose);
        // 类型
        llType = view.findViewById(R.id.llType);
        // 结束通航
        tvFinish = view.findViewById(R.id.tvFinish);
        // 取消报港
        tvCancel = view.findViewById(R.id.tvCancel);
        // 3个月
        tvThree = view.findViewById(R.id.tvThree);
        // 3个月
        tvSix = view.findViewById(R.id.tvSix);
        // 3个月
        tvOneYear = view.findViewById(R.id.tvOneYear);
        // 开始时间
        tvStartTime = view.findViewById(R.id.tvStartTime);
        // 结束时间
        tvEndTime = view.findViewById(R.id.tvEndTime);
        // 重置
        tvReset = view.findViewById(R.id.tvReset);
        // 查询
        tvQuery = view.findViewById(R.id.tvQuery);

        // 判断订单类型
        if (orderType == 1) {
            llType.setVisibility(View.VISIBLE);
        } else if (orderType == 2) {
            llType.setVisibility(View.GONE);
        }

        // 类型
        setDefault(mContext, 1);
        type = selectType;
        if (selectType == 0) {
            tvFinish.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvFinish.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvFinish.setTypeface(Typeface.DEFAULT_BOLD);
        } else if (selectType == 1) {
            tvCancel.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvCancel.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvCancel.setTypeface(Typeface.DEFAULT_BOLD);
        }

        // 时间区间
        setDefault(mContext, 2);
        if (!TextUtil.isEmpty(selectTime)) {
            time = selectTime;
            if ("threeM".equals(selectTime)) {
                tvThree.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
                tvThree.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
                tvThree.setTypeface(Typeface.DEFAULT_BOLD);
            } else if ("sixM".equals(selectTime)) {
                tvSix.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
                tvSix.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
                tvSix.setTypeface(Typeface.DEFAULT_BOLD);
            } else if ("oneY".equals(selectTime)) {
                tvOneYear.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
                tvOneYear.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
                tvOneYear.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                setDefault(mContext, 2);
            }
        }

        // 开始时间 结束时间
        setDefault(mContext, 3);
        if (!TextUtil.isEmpty(selectStartTime)) {
            startTime = selectStartTime.split(" ", -1)[0];
            tvStartTime.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvStartTime.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvStartTime.setTypeface(Typeface.DEFAULT_BOLD);
            tvStartTime.setText(startTime);
        }
        if (!TextUtil.isEmpty(selectEndTime)) {
            endTime = selectEndTime.split(" ", -1)[0];
            tvEndTime.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvEndTime.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvEndTime.setTypeface(Typeface.DEFAULT_BOLD);
            tvEndTime.setText(endTime);
        }

        // 点击效果
        ivClose.setOnClickListener(v -> {
            dismiss();
        });
        tvFinish.setOnClickListener(v -> {
            setDefault(mContext, 1);
            type = 0;
            tvFinish.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvFinish.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvFinish.setTypeface(Typeface.DEFAULT_BOLD);
        });
        tvCancel.setOnClickListener(v -> {
            setDefault(mContext, 1);
            type = 1;
            tvCancel.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvCancel.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvCancel.setTypeface(Typeface.DEFAULT_BOLD);
        });
        tvThree.setOnClickListener(v -> {
            setDefault(mContext, 2);
            setDefault(mContext, 3);
            time = "threeM";
            tvThree.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvThree.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvThree.setTypeface(Typeface.DEFAULT_BOLD);
        });
        tvSix.setOnClickListener(v -> {
            setDefault(mContext, 2);
            setDefault(mContext, 3);
            time = "sixM";
            tvSix.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvSix.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvSix.setTypeface(Typeface.DEFAULT_BOLD);
        });
        tvOneYear.setOnClickListener(v -> {
            setDefault(mContext, 2);
            setDefault(mContext, 3);
            time = "oneY";
            tvOneYear.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
            tvOneYear.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
            tvOneYear.setTypeface(Typeface.DEFAULT_BOLD);
        });
        tvStartTime.setOnClickListener(v -> {
            // 弹出选择时间的弹窗
            if (chooseDateDialog == null) {
                chooseDateDialog = new ChooseDateDialog(mContext);
                chooseDateDialog.setTitle("开始时间");
                if (TextUtil.isEmpty(startTime)) {
                    int year = Calendar.getInstance().get(Calendar.YEAR);
                    int month = Calendar.getInstance().get(Calendar.MONTH);
                    int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
                    chooseDateDialog.setDate(year, month, day);
                } else {
                    String[] split = startTime.split("-", -1);
                    int year = Integer.parseInt(split[0]);
                    int month = Integer.parseInt(split[1]) - 1;
                    int day = Integer.parseInt(split[2]);
                    chooseDateDialog.setDate(year, month, day);
                }
                chooseDateDialog.setOnClickBtnListener(new ChooseDateDialog.OnClickBtnListener() {
                    @Override
                    public void onConfirm(ChooseDateDialog dialog) {
                        setDefault(mContext, 2);
                        // 得到选择的时间
                        int[] timeInt = chooseDateDialog.getTime();
                        tvStartTime.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
                        tvStartTime.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
                        tvStartTime.setTypeface(Typeface.DEFAULT_BOLD);
                        tvStartTime.setText(String.format(Locale.CHINA, "%d-%d-%d", timeInt[0], timeInt[1], timeInt[2]));
                        startTime = timeInt[0] + "-" + (timeInt[1] < 10 ? ("0" + timeInt[1]) : timeInt[1]) + "-" + (timeInt[2] < 10 ? ("0" + timeInt[2]) : timeInt[2]);
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }

                    @Override
                    public void onCancel(ChooseDateDialog dialog) {
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }

                    @Override
                    public void onOutside(ChooseDateDialog dialog) {
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }
                });
                chooseDateDialog.setCancel(false);
                chooseDateDialog.showDialog();
            }
        });
        tvEndTime.setOnClickListener(v -> {
            // 弹出选择时间的弹窗
            if (chooseDateDialog == null) {
                chooseDateDialog = new ChooseDateDialog(mContext);
                chooseDateDialog.setTitle("终止时间");
                if (TextUtil.isEmpty(endTime)) {
                    int year = Calendar.getInstance().get(Calendar.YEAR);
                    int month = Calendar.getInstance().get(Calendar.MONTH);
                    int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
                    chooseDateDialog.setDate(year, month, day);
                } else {
                    String[] split = endTime.split("-", -1);
                    int year = Integer.parseInt(split[0]);
                    int month = Integer.parseInt(split[1]) - 1;
                    int day = Integer.parseInt(split[2]);
                    chooseDateDialog.setDate(year, month, day);
                }
                chooseDateDialog.setOnClickBtnListener(new ChooseDateDialog.OnClickBtnListener() {
                    @Override
                    public void onConfirm(ChooseDateDialog dialog) {
                        setDefault(mContext, 2);
                        // 得到选择的时间
                        int[] timeInt = chooseDateDialog.getTime();
                        tvEndTime.setBackgroundResource(R.drawable.bg_all_eef7ff_corner_6);
                        tvEndTime.setTextColor(Utils.getColor(mContext, R.color.blue_008cff));
                        tvEndTime.setTypeface(Typeface.DEFAULT_BOLD);
                        tvEndTime.setText(String.format(Locale.CHINA, "%d-%d-%d", timeInt[0], timeInt[1], timeInt[2]));
                        endTime = timeInt[0] + "-" + (timeInt[1] < 10 ? ("0" + timeInt[1]) : timeInt[1]) + "-" + (timeInt[2] < 10 ? ("0" + timeInt[2]) : timeInt[2]);
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }

                    @Override
                    public void onCancel(ChooseDateDialog dialog) {
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }

                    @Override
                    public void onOutside(ChooseDateDialog dialog) {
                        dialog.dismiss();
                        chooseDateDialog = null;
                    }
                });
                chooseDateDialog.setCancel(false);
                chooseDateDialog.showDialog();
            }
        });
        tvReset.setOnClickListener(v -> {
            setDefault(mContext, -1);
        });
        tvQuery.setOnClickListener(v -> {
            if (!TextUtil.isEmpty(startTime) && !TextUtil.isEmpty(endTime)) {
                String[] startTimeList = startTime.split("-", -1);
                int startYear = Integer.parseInt(startTimeList[0]);
                int startMonth = Integer.parseInt(startTimeList[1]);
                int startDay = Integer.parseInt(startTimeList[2]);
                String[] endTimeList = endTime.split("-", -1);
                int endYear = Integer.parseInt(endTimeList[0]);
                int endMonth = Integer.parseInt(endTimeList[1]);
                int endDay = Integer.parseInt(endTimeList[2]);
                if (startYear > endYear) {
                    ToastUtils.getInstance().toast("开始时间不能大于终止时间");
                    return;
                } else if (startYear == endYear) {
                    if (startMonth > endMonth) {
                        ToastUtils.getInstance().toast("开始时间不能大于终止时间");
                        return;
                    } else if (startMonth == endMonth) {
                        if (startDay > endDay) {
                            ToastUtils.getInstance().toast("开始时间不能大于终止时间");
                            return;
                        }
                    }
                }
            } else if (!TextUtil.isEmpty(startTime) && TextUtil.isEmpty(endTime)) {
                ToastUtils.getInstance().toast("请选择终止时间");
                return;
            } else if (!TextUtil.isEmpty(endTime) && TextUtil.isEmpty(startTime)) {
                ToastUtils.getInstance().toast("请选择开始时间");
                return;
            }
            if (onQueryListener != null) {
                String start = "";
                if (!TextUtil.isEmpty(startTime)) {
                    start = startTime + " 00:00:00";
                }
                String end = "";
                if (!TextUtil.isEmpty(endTime)) {
                    end = endTime + " 23:59:59";
                }
                if (TextUtil.isEmpty(startTime) && TextUtil.isEmpty(endTime) && type == -1 && TextUtil.isEmpty(time)) {
                    onQueryListener.onQuery(this, false, start, end, type, time);
                } else {
                    onQueryListener.onQuery(this, true, start, end, type, time);
                }
            }
        });
    }

    /**
     * 配置默认展示
     *
     * @param mContext
     * @param tag
     */
    private void setDefault(Context mContext, int tag) {
        if (tag == 1 || tag == -1) {
            tvFinish.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvFinish.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvFinish.setTypeface(Typeface.DEFAULT);
            tvCancel.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvCancel.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvCancel.setTypeface(Typeface.DEFAULT);

            type = -1;
        }
        if (tag == 2 || tag == -1) {
            tvThree.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvThree.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvThree.setTypeface(Typeface.DEFAULT);
            tvSix.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvSix.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvSix.setTypeface(Typeface.DEFAULT);
            tvOneYear.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvOneYear.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvOneYear.setTypeface(Typeface.DEFAULT);

            time = "";
        }
        if (tag == 3 || tag == -1) {
            tvStartTime.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvStartTime.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvStartTime.setText("开始时间");
            tvStartTime.setTypeface(Typeface.DEFAULT);
            tvEndTime.setBackgroundResource(R.drawable.bg_gray_f1f5f7_corner_6);
            tvEndTime.setTextColor(Utils.getColor(mContext, R.color.black_464646));
            tvEndTime.setText("终止时间");
            tvEndTime.setTypeface(Typeface.DEFAULT);

            startTime = "";
            endTime = "";
        }
    }
}
